<?php
/**
 * Created by PhpStorm.
 * User: lscm
 * Date: 14-7-9
 * Time: 下午1:22
 */
require("ad.php");
$dbServer = "localhost";
$dbUser = "adsAdmin";
$dbPassword = "";
$dbName = "surfingHelper";
$con=mysqli_connect($dbServer,$dbUser,$dbPassword, $dbName);

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

function getAdByPicUrl($picUrl){
    $picArray = array();
    global $adArr;
    foreach($adArr as $ad){
        if($ad->picUrl == $picUrl){
            array_push($picArray, $ad);
        }
    }
    return $picArray;
}
function getAdByTargetUrl($targetUrl){
    $targetArray = array();
    global $adArr;
    foreach($adArr as $ad){
        if($ad->targetUrl == $targetUrl){
            array_push($targetArray, $ad);
        }
    }
    return $targetArray;
}
function getAdByCategory($category){
    global $con;
    $result = mysqli_query($con,"SELECT * FROM ads WHERE AD_CATEGORY='game'");
    for ($res = array(); $tmp = $result->fetch_array(MYSQLI_ASSOC);) $res[] = $tmp;
    return $res;
}
$retAdArray = getAdByCategory("game");
$len = count($retAdArray);


header('Content-type: application/json');
//获取回调函数名
$callback = $_REQUEST ['callback'];

if (isset($_COOKIE["name"])){
    $name =  $_COOKIE["name"];
}else{
    $name = "chopper";
}

//json数据
$json = json_encode($retAdArray);

//echo $json;
//echo $callback;

//输出：回调函数名(json数据)
echo ($callback . "(" . $json . ")");
//for($i = 0; $i < $len; $i++){
//    echo("<a href='".$retAdArray[$i][ad_target_url]."'><img src='".$retAdArray[$i][ad_pictures_url]."'></a>");
//}
mysqli_close($con);

